package com.logic.modular.game.battle.process.skillEffectHandle.base;

import com.logic.modular.game.battle.panel.Battle;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffect;

import com.logic.modular.game.battle.panel.skillEffect.domain.BaseEffectValueInfo;
import com.logic.modular.game.battle.panel.skillEffect.domain.LayerValue;
import com.logic.modular.game.battle.panel.skillEffect.domain.SkillEffectLayer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * 处理
 * 数值|叠层
 */
@Slf4j
@Component
public class EffectHandle4 {
    /**
     * 处理
     * 数值|叠层
     */
    public void tryHandleLayerValue(PvpSkillEffect pvpSkillEffect) {
        Integer status = pvpSkillEffect.getStatus().getStatus();
        if (pvpSkillEffect.status.getIsTrigger() != 1) {
            return;

        }
        BaseEffectValueInfo baseEffectValueInfo = pvpSkillEffect.getBaseEffectValueInfo();
        Double decayValue = baseEffectValueInfo.getDecayValue();
        Double triggerValue = baseEffectValueInfo.getTriggerValue();
        SkillEffectLayer skillEffectLayer = pvpSkillEffect.getSkillEffectLayer();

        Map<Integer, LayerValue> layerValueMap = skillEffectLayer.getLayerValueMap();
        if (layerValueMap.isEmpty()) { // 新增数值
            String battleUUID = pvpSkillEffect.getBattleUUID();
            Battle battle = Battle.get(battleUUID);
            Integer round = battle.getRound();
            int targetLayerNumber = layerValueMap.size() + 1;
            LayerValue build = LayerValue.builder()
                    .layerNumber(targetLayerNumber)
                    .layerRound(round)
                    .refreshRound(0)
                    .triggerCount(1)
                    .thisDecayValue(0.0)
                    .totalDecayValue(0.0)
                    .lastChangeValue(0.0)
                    .thisChangeValue(triggerValue)
                    .totalChangeValue(triggerValue)
                    .validTriggerCount(0)
                    .build();
            layerValueMap.put(targetLayerNumber, build);
            skillEffectLayer.setLayerValueMap(layerValueMap);
            skillEffectLayer.obtainThisChangeValue();
            pvpSkillEffect.setSkillEffectLayer(skillEffectLayer);
        } else {
            LayerValue layerValue = layerValueMap.get(1);
            if (Objects.nonNull(layerValue)) {
                layerValue.validTriggerCount += 1;
            }
            if (decayValue < 0.0) {
                layerValueMap.forEach((key, layValue) -> {
                    LayerValue needDecayValue = layerValueMap.get(key);
                    needDecayValue.refreshDecayValue(decayValue);
                    needDecayValue.validTriggerCount += 1;
                    layerValueMap.put(key, needDecayValue);
                });
            } else {
                layerValue.setThisChangeValue(triggerValue);
            }
        }
    }
}
